from lib.ZNode import ZNode


class Sway ( ZNode ) :
	def __init__ ( self , father , image ) :
		self.image = image
		rect = self.image.get_rect ( )
		super ( Sway , self ).__init__ ( father , rect )
		self.offer = 4
		self.father = father
		self._pos = 0 , 0
		self.turn = 1
		self.speed = 2
		self._running = False
		self.min = self._pos [ 0 ] - self.offer
		self.max = self._pos [ 0 ] + self.offer
		self.default = self._pos [ 0 ]
		self.current = 0
		self.director = "right"

	def draw ( self ) :
		if self._running :
			if self.__orer_town ( ) :
				if self.director == "left" :
					self.rect.left -= self.speed
				elif self.director == "right" :
					self.rect.left += self.speed
			else :
				self.rect.left = self.default
				self._running = False

			if self.rect.left >= self.max :
				self.director = "left"
			if self.rect.left <= self.min :
				self.director = "right"
				self.current += 1
			self.father.screen.blit ( self.image , self._pos )

	def __orer_town ( self ) :
		return False if self.current >= self.turn else True
